package com.nut.spp.api.client;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.spp.api.req.ShopQueryReq;
import com.nut.spp.common.Holder;
import com.nut.spp.common.Result;
import com.nut.spp.entity.Shop;
import com.nut.spp.interceptor.ClientJwt;
import com.nut.spp.service.ShopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/client/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @GetMapping(value = "list")
    public Result<IPage<Shop>> list(ShopQueryReq req) {
        return shopService.listPage(req);
    }

    @GetMapping(value = "/{id}")
    public Result<Shop> detail(@PathVariable Integer id) {
        return shopService.getById(id);
    }

//    @GetMapping(value = "/my")
//    public Result<Shop> my() {
//        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
//        return shopService.getByMemberId(clientJwt.getMemberId(), clientJwt.getShopId());
//    }

//    @PostMapping
//    public Result<Integer> add(@RequestBody Shop shop) {
//
//        ClientJwt clientJwt = Holder.CLIENT_JWT_THREAD_LOCAL.get();
//
//        Integer shopId = clientJwt.getShopId();
//        if (shopId != null) {
//            return Result.fail("已有店铺");
//        }
//
//        shop.setMemberId(clientJwt.getMemberId());
//        shop.setMobile(clientJwt.getMobile());
//        shop.setOwn(0);
//
//        return shopService.add(shop);
//    }

    @PostMapping("submit/{shopId}")
    public Result submit(@PathVariable Integer shopId) {
        return shopService.submit(shopId);
    }

    @GetMapping(value = "/queryCheckStatus/{shopId}")
    public Result<Shop> queryCheckStatus(@PathVariable Integer shopId) {
        return shopService.queryCheckStatus(shopId);
    }

    @PutMapping
    public Result update(@RequestBody Shop shop) {
        Long memberId = Holder.CLIENT_JWT_THREAD_LOCAL.get().getMemberId();
        shop.setMemberId(memberId);
        return shopService.updateById(shop);
    }

}